<?php
/* SVN FILE: $Id: version_upgrade.php 225 2009-09-18 03:41:55Z supersmashbrothers $ */
/**
* ibMarket 2
* IP.Board 3.0
* IP.Board ibMarket upgrader
*
* @author $Author: supersmashbrothers $
* @copyright $Copyright$
* @version $Revision: 225 $
* @lastrevision $Date: 2009-09-18 03:41:55 +0000 (Fri, 18 Sep 2009) $
* @modifiedby $LastChangedBy: supersmashbrothers $
* @lastmodified $LastChangedDate: 2009-09-18 03:41:55 +0000 (Fri, 18 Sep 2009) $
* @filesource $URL: http://ipb-ibmarket.googlecode.com/svn/ibmarket2/trunk/admin/applications_addon/other/ibmarket/setup/versions/upg_20000/version_upgrade.php $
*/

if ( ! defined( 'IN_IPB' ) )
{
	print "<h1>Incorrect access</h1>You cannot access this file directly. If you have recently upgraded, make sure you upgraded all the relevant files.";
	exit();
}

class version_upgrade
{
	/**
	 * Custom HTML to show
	 *
	 * @access	private
	 * @var		string
	 */
	private $_output = '';
	
	/**
	* fetchs output
	* 
	* @access	public
	* @return	string
	*/
	public function fetchOutput()
	{
		return $this->_output;
	}
	
	/**
	 * Execute selected method
	 *
	 * @access	public
	 * @param	object		Registry object
	 * @return	void
	 */
	public function doExecute( ipsRegistry $registry ) 
	{
		/* Make object */
		$this->registry =  $registry;
		$this->DB       =  $this->registry->DB();
		$this->settings =& $this->registry->fetchSettings();
		$this->request  =& $this->registry->fetchRequest();
		$this->cache    =  $this->registry->cache();
		$this->caches   =& $this->registry->cache()->fetchCaches();
		
		//--------------------------------
		// What are we doing?
		//--------------------------------

		switch( $this->request['workact'] )
		{
			default:
			case 'categoryseo':
				$this->updateCategorySEO();
				break;
			case 'pluginseo':
				$this->updatePluginSEO();
				break;
			case 'download':
				$this->updateDownloadItem();
				break;
			case 'multipleeffects':
				$this->updateMultipleEffectItem();
				break;
			case 'nameeffects':
				$this->updateNameEffectItem();
				break;
		}
		
		/* Workact is set in the function, so if it has not been set, then we're done. The last function should unset it. */
		if ( $this->request['workact'] )
		{
			return false;
		}
		else
		{
			return true;
		}
	}
	
	/**
	* 
	* 
	* @access	public
	* @param	int
	*/
	public function updateCategorySEO()
	{
		$this->DB->build(array('select'=>'*','from'=>'market_categories'));
		$q = $this->DB->execute();
		while($category = $this->DB->fetch($q))
		{
			$this->DB->update('market_categories',array('seo_name'=>IPSText::makeSeoTitle($category['cat_name'])),"cat_id=".$category['cat_id']);
		}
		ipsRegistry::cache()->rebuildCache('market_category','ibmarket');
		
		/* Next Page */
		$this->request['workact'] = 'pluginseo';
	}
	
	/**
	* 
	* 
	* @access	public
	* @param	int
	*/
	public function updatePluginSEO()
	{
		$this->DB->build(array('select'=>'*','from'=>'market_plugins'));
		$q = $this->DB->execute();
		while($plugin = $this->DB->fetch($q))
		{
			$this->DB->update('market_plugins',array('seo_name'=>IPSText::makeSeoTitle($plugin['name'])),"link='".$plugin['link']."'");
		}
		ipsRegistry::cache()->rebuildCache('market_plugin','ibmarket');
		
		/* Next Page */
		$this->request['workact'] = 'download';
	}
	
	/**
	* 
	* 
	* @access	public
	* @param	int
	*/
	public function updateDownloadItem()
	{
		$this->DB->build(array('select'=>'*','from'=>'market_items','where'=>"filename='/item_downloadfile.php'"));
		$q = $this->DB->execute();
		while($item = $this->DB->fetch($q))
		{
			$settings = unserialize($item['item_config']);
			$parts = explode("/", $settings['location']);
			$settings['location'] = array_pop($parts);
			$this->DB->update('market_items',array('item_config'=>serialize($settings)),"item_id=".$item['item_id']);
		}
		
		/* Next Page */
		$this->request['workact'] = 'multipleeffects';
	}
	
	/**
	* 
	* 
	* @access	public
	* @param	int
	*/
	public function updateMultipleEffectItem()
	{
		if($item = $this->DB->buildAndFetch(array('select'=>'*','from'=>'market_items','where'=>"filename='own/item_multipleeffect.php'")))
		{
			$this->DB->addField("profile_portal","multiple_effect","TEXT NOT NULL","''");
			$this->DB->build(array('select'=>'*','from'=>'members','where'=>"multiple_effect != ''"));
			$q = $this->DB->execute();
			while($member = $this->DB->fetch($q))
			{
				$data = array('multiple_effect' => $member['multiple_effect']);
				$check = ipsRegistry::DB()->buildAndFetch( array( 'select' => 'pp_member_id', 'from' => 'profile_portal', 'where' => 'pp_member_id=' . $member['member_id'] ) );
				
				if( !$check['pp_member_id'] )
				{
					$data['pp_member_id'] = $member['member_id'];
					ipsRegistry::DB()->insert( 'profile_portal', $data );
				}
				else
				{
					ipsRegistry::DB()->update( 'profile_portal', $data, 'pp_member_id=' . $member['member_id'] );
				}
			}
			$this->DB->dropField("members","multiple_effect");
		}
		
		/* Next Page */
		$this->request['workact'] = 'nameeffects';
	}
	
	/**
	* 
	* 
	* @access	public
	* @param	int
	*/
	public function updateNameEffectItem()
	{
		if($item = $this->DB->buildAndFetch(array('select'=>'*','from'=>'market_items','where'=>"filename='own/item_multipleeffect.php'")))
		{
			$this->DB->addField("profile_portal","effect_prefix","varchar(255) NOT NULL","''");
			$this->DB->addField("profile_portal","effect_suffix","varchar(255) NOT NULL","''");
			$this->DB->build(array('select'=>'*','from'=>'members','where'=>"effect_prefix != ''"));
			$q = $this->DB->execute();
			while($member = $this->DB->fetch($q))
			{
				$prefix = '';
				if($member['effect_prefix'] == '<b>')
				{
					$prefix = "<span style=\"font-weight:bold\">";
				}
				elseif($member['effect_prefix'] == '<i>')
				{
					$prefix = "<span style=\"font-style:italic\">";
				}
				elseif($member['effect_prefix'] == 'color')
				{
					$prefix = $member['effect_prefix'];
				}
				elseif($member['effect_prefix'] == 'background')
				{
					$prefix = $member['effect_prefix'];
				}
				elseif(strpos($member['effect_prefix'],"<div style=\"width:100%; filter:dropshadow(color='") === 0)
				{
					$prefix = "<span style=\"display:inline-block; filter:dropshadow(color=".substr($member['effect_prefix'],49,strpos($member['effect_prefix'],"',")-49).", offx=5, offy=-3, Positive=true)\">";
				}
				elseif(strpos($member['effect_prefix'],"<div style=\"width:100%; filter:shadow(color='") === 0)
				{
					$prefix = "<span style=\"display:inline-block; filter:shadow(color=".substr($member['effect_prefix'],45,strpos($member['effect_prefix'],"',")-45).", direction=45)\">";
				}
				elseif(strpos($member['effect_prefix'],"<div style=\"width:100%; filter:glow(color='") === 0)
				{
					$prefix = "<span style=\"display:inline-block; filter:glow(color=".substr($member['effect_prefix'],43,strpos($member['effect_prefix'],"',")-43).", strength=6)\">";
				}
				elseif($member['effect_prefix'] == "<div style=\"width:100%; filter:blur(add=false, direction=140, strength=6)\">")
				{
					$prefix = "<span style=\"display:inline-block; filter:blur(add=false, direction=140, strength=6)\">";
				}
				elseif($member['effect_prefix'] == "<div style=\"width:100%; filter:flipv\">")
				{
					$prefix = "<span style=\"display:inline-block; filter:flipv\">";
				}
				elseif($member['effect_prefix'] == "<div style=\"width:100%; filter:fliph\">")
				{
					$prefix = "<span style=\"display:inline-block; filter:fliph\">";
				}
				elseif($member['effect_prefix'] == "<div style=\"width:100%; filter:wave(strength=2)\">")
				{
					$prefix = "<span style=\"display:inline-block; filter:wave(strength=2)\">";
				}
				elseif($member['effect_prefix'] == "<div style=\"width:100%; filter:xray\">")
				{
					$prefix = "<span style=\"display:inline-block; filter:xray\">";
				}
				elseif($member['effect_prefix'] == "<div style=\"width:100%; filter:invert\">")
				{
					$prefix = "<span style=\"display:inline-block; filter:invert\">";
				}
				$suffix = $prefix ? "</span>" : "";
				
				$data = array('effect_prefix' => $prefix,'effect_suffix' => $suffix);
				$check = ipsRegistry::DB()->buildAndFetch( array( 'select' => 'pp_member_id', 'from' => 'profile_portal', 'where' => 'pp_member_id=' . $member['member_id'] ) );
				
				if( !$check['pp_member_id'] )
				{
					$data['pp_member_id'] = $member['member_id'];
					ipsRegistry::DB()->insert( 'profile_portal', $data );
				}
				else
				{
					ipsRegistry::DB()->update( 'profile_portal', $data, 'pp_member_id=' . $member['member_id'] );
				}
			}
			$this->DB->dropField("members","effect_prefix");
			$this->DB->dropField("members","effect_suffix");
		}
		
		/* Next Page */
		$this->request['workact'] = '';
	}
}
	
?>